﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KingdeeWebServiceProject.Utils
{
    public static class ObjectUtil
    {
        public static string ToRelString(this object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return "";
            }
            return value.ToString();
        }


        public static int ToInt32(this object value)
        {
            int newValue = 0;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            int.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static long ToInt64(this object value)
        {
            long newValue = 0L;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            long.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static double ToDouble(this object value)
        {
            double newValue = 0d;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            double.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static decimal ToDecimal(this object value)
        {
            decimal newValue = 0;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            decimal.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static DateTime ToDateTime(this object value)
        {
            DateTime newValue = DateTime.MinValue;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            DateTime.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static DateTime ToDateTime(this object value, string format)
        {
            DateTime newValue = DateTime.MinValue;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            DateTime.TryParseExact(value.ToString(), format, null, System.Globalization.DateTimeStyles.None, out newValue);
            return newValue;
        }

        public static bool ToBoolean(this object value)
        {
            bool newValue = false;

            if (value == null || value == DBNull.Value)
            {
                return newValue;
            }

            bool.TryParse(value.ToString(), out newValue);
            return newValue;
        }

        public static bool IsEmptyValue(this object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return true;
            }

            if (string.IsNullOrEmpty(value.ToString()))
            {
                return true;
            }
            if (string.IsNullOrWhiteSpace(value.ToString()))
            {
                return true;
            }

            return false;
        }
    }
}
